package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 所有的更新都用post方法
 * 查询用get方法
 *
 * @WebServlet("/back/user/add")
 * @WebServlet("/back/user/update")
 * @WebServlet("/back/user/delete")
 * @WebServlet("/back/user/findById")
 * @WebServlet("/back/user/findAll")
 * @WebServlet("/back/user/getCount")
 */
@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
        String username = req.getParameter("name");
        String imgHref = req.getParameter("imgHref");

//        3.封装model
        ProductModel productModel = new ProductModel();

//        4.调用服务逻辑层
//        UserService userService = new UserServiceImpl();
        ProductService productService = new ProductServiceImpl();
        ResultModel resultModel = productService.add(productModel);
//        5.返回结果给前端
        resp.getWriter().print(JSONObject.toJSONString(resultModel));
    }
}
